package com.lxk.mall.pms.mapper;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.mall.pms.domain.PmsProductBrowseHistory;
import com.lxk.mall.pms.domain.vo.history.PmsProductBrowseHistoryVo;
import org.dromara.common.mybatis.core.domain.BaseEntity;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;

import java.util.Collection;

/**
 * 商品浏览记录Mapper接口
 *
 * @author Lion Li
 * @date 2024-06-06
 */
public interface PmsProductBrowseHistoryMapper extends BaseMapperPlus<PmsProductBrowseHistory, PmsProductBrowseHistoryVo> {

    default void updateUserDeletedByUserId(Long userId, Collection<Long> spuIds, Boolean userDeleted) {
        update(new LambdaUpdateWrapper<PmsProductBrowseHistory>()
            .eq(PmsProductBrowseHistory::getUserId, userId)
            .in(CollUtil.isNotEmpty(spuIds), PmsProductBrowseHistory::getSpuId, spuIds)
            .set(PmsProductBrowseHistory::getDelFlag, userDeleted));
    }

    default PmsProductBrowseHistory selectByUserIdAndSpuId(Long userId, Long spuId) {
        return selectOne(new LambdaQueryWrapper<PmsProductBrowseHistory>()
            .eq(PmsProductBrowseHistory::getUserId, userId)
            .eq(PmsProductBrowseHistory::getSpuId, spuId)
            .last("limit 1"));
    }

    default Page<PmsProductBrowseHistory> selectPageByUserIdOrderByCreateTimeAsc(Long userId, Integer pageNo, Integer pageSize) {
        Page<PmsProductBrowseHistory> page = Page.of(pageNo, pageSize);
        LambdaQueryWrapper<PmsProductBrowseHistory> lqw = new LambdaQueryWrapper<>();
        if (userId != null) {
            lqw.eq(PmsProductBrowseHistory::getUserId, userId);
        }
        lqw.orderByAsc(BaseEntity::getCreateTime);
        return selectPage(page, lqw);
    }
}
